const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const removeEndPoint = (n = 0) => {
  var n = 3.00
  console.log(n);
  // n = n.toString()
  // return n[1] ? n : `0${n}`
}

const screenView = (target='page') => { //判断元素是否出现可视内
  const {
    windowHeight,windowWidth
  } = wx.getSystemInfoSync()

  setInterval(() => {
    const query = wx.createSelectorQuery()
    // query.select('#myElement').boundingClientRect()
    query.select(target).boundingClientRect()

    query.exec(rect => {
      const {
        top,
        height
      } = rect[0]

      // windowHeight = top（目标元素刚进入可视区域）
      // windowHeight - top = height（目标元素完全进入可视区域）
      // top = 0 (目标元素刚离开可视区域)
      // top + height = 0 （目标元素完全离开可视区域 ）

      if ((top < windowHeight) && (top + height > 0)) {
        console.log('元素在可视区域出现')
      } else {
        console.log('元素在可视区域消失')
      }
    })
  }, 2000)
}



module.exports = {
  formatTime,
  removeEndPoint,
  screenView
}